home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / compress / tar321__.zip / SOURCES.ZIP / ROLL.C < prev    next >
C/C++ Source or Header  |  1997-03-14  |  6KB  |  234 lines

  1. /* roll.c - sequetial read/write cache programs
  2.  * This is the part of the Tar program (see file tar.c)
  3.  * Author: T.V.Shaporev
  4.  * Prepared for release 19 Oct 1990
  5.  * Called by store() (see file store.c)
  6.  */
  7. #include "sysup.h"
  8.  
  9. #include <stdio.h>
  10. #ifdef TRACE
  11.     extern FILE *myout;
  12. #    define Printf  (void)printf
  13. #endif
  14.  
  15. #ifdef MSDOS
  16. #   include <string.h>
  17. #   include <stdlib.h>
  18. #   include "longdos.h"
  19. #   define CREATE(x) lopen4((x),2,18,0)
  20. #   define FREE(x)   free(x)
  21. #else
  22. #   define CREATE(x) creat((x),0600)
  23. #   define FREE(x)   free((char *)(x))
  24.  
  25.     int  strlen();
  26.     char *malloc(), *strncpy(), *strncat(), *getenv(), *mktemp();
  27.     int  creat(), read(), write(), close(), unlink();
  28.     long lseek();
  29.     void free();
  30. #endif
  31.  
  32. #ifdef UNIX
  33. #    define TMPDIR   "/usr/tmp"
  34. #       define DIR      "/"
  35. #    define DEV      '\0'
  36. #    define MAXFNAME 512 /* Analagous to MAXNAMSIZ */
  37. #endif
  38.  
  39. #ifdef VMS
  40. #    define TMPDIR   "SYS$SCRATCH:"
  41. #       define DIR      "]"
  42. #    define DEV      ':'
  43. #       define MAXFNAME 512 /* ??? */
  44. #endif
  45.  
  46. #ifdef MSDOS
  47. #    define TMPDIR   ""
  48. #       define DIR      "\\"
  49. #    define DEV      ':'
  50. #    define MAXFNAME PATHSIZE
  51. #endif
  52.  
  53. #define MAXPAGE  64   /* max cash is 0.5M */
  54. #define PAGESIZE 8192 /* Be careful !!! */
  55.  
  56. typedef struct {
  57.                   unsigned char * page[MAXPAGE];
  58.                   int             poz, npage, maxpage;
  59.                   char            name[MAXFNAME+1];
  60.                   int             handle;
  61.                } roll;
  62.  
  63. #include "roll.h"
  64.  
  65. static roll *r = (roll *)0;
  66.  
  67. int newroll(templ)
  68. char *templ;
  69. {
  70.    extern char *mktemp();
  71.    register i;
  72.    char *e;
  73.  
  74.    if (r) return 0;
  75.  
  76.    r = (roll *)malloc(sizeof(roll));
  77.    if (!r) return -1;
  78.    (r->page)[0] = (unsigned char *)malloc(PAGESIZE);
  79.    if (!((r->page)[0])) {
  80.       FREE((char *)r);
  81.       return -1;
  82.    }
  83.    for (i=1; i<MAXPAGE; i++) (r->page)[i] = (unsigned char *)0;
  84.    r->maxpage = MAXPAGE - 1;
  85.    r->npage   = 0;
  86.    r->poz     = 0;
  87.  
  88.    if ((e = getenv("TMP")) == (char *)0) {
  89.       (void)strncpy(r->name, TMPDIR, MAXFNAME);
  90.    } else {
  91.       (void)strncpy(r->name, e, MAXFNAME);
  92.    }
  93. #ifndef VMS
  94.    i = strlen(r->name) - 1;
  95.    if (r->name[i]!= *DIR
  96. #     ifdef DEV
  97.                          && r->name[i]!=DEV
  98. #     endif
  99.       ) (void)strncat(r->name,DIR,MAXFNAME);
  100. #endif
  101.    (void)mktemp(strncat(r->name, templ, MAXFNAME));
  102.  
  103.    r->handle  = -1;
  104. #ifdef TRACE
  105.   Printf("new roll: poz = %4d page = %d max = %2d handle = %d\n",
  106.                     r->poz,   r->npage, r->maxpage, r->handle);
  107. #endif
  108.    return 0;
  109. }
  110.  
  111. void delroll()
  112. {
  113.    register i;
  114.  
  115.    if (!r) return;
  116. #ifdef TRACE
  117.   Printf("del roll: poz = %4d page = %d max = %2d handle = %d\n",
  118.                     r->poz,   r->npage, r->maxpage, r->handle);
  119. #endif
  120.    for (i=0; i<MAXPAGE && (r->page)[i]; i++) FREE((r->page)[i]);
  121.    if (r->handle >= 0) {
  122.       (void)close (r->handle);
  123.       (void)unlink(r->name);
  124.    }
  125.    r = (roll *)0;
  126. }
  127.  
  128. int rewroll(flread)
  129. int flread;
  130. {
  131. #ifdef TRACE
  132.    Printf("rew roll: poz = %4d page = %d max = %2d handle = %d\n",
  133.                      r->poz,   r->npage, r->maxpage, r->handle);
  134. #endif
  135.    if (r->handle >= 0 && r->npage >= r->maxpage) {
  136.       if (write(r->handle,(char*)((r->page)[r->maxpage]),PAGESIZE)!=PAGESIZE)
  137.      return -1;
  138.       if (lseek(r->handle, 0L, 0) < 0) return -1;
  139.       if (flread && r->maxpage < 1) {
  140.      if (read(r->handle, (char*)((r->page)[0]), PAGESIZE) != PAGESIZE)
  141.         return -1;
  142.       }
  143.    }
  144.    r->npage = 0;
  145.    r->poz   = 0;
  146.    return 0;
  147. }
  148.  
  149. int rputc(i)
  150. int i;
  151. {
  152.    register unsigned char *p;
  153.  
  154.    if (r->poz >= PAGESIZE) {
  155. #ifdef TRACE
  156.      Printf("  roll 1: poz = %4d page = %d max = %2d handle = %d\n",
  157.                        r->poz,   r->npage, r->maxpage, r->handle);
  158. #endif
  159.       if (r->npage < r->maxpage) {
  160.          p = (r->page)[(r->npage)+1];
  161.          if (p) {
  162.             r->npage += 1;
  163.          } else if (r->handle < 0) {
  164.             p = (unsigned char *)malloc(PAGESIZE);
  165.         if (p) {
  166.            (r->page)[++(r->npage)] = p;
  167.            r->poz = 0;
  168.         } else {
  169.            r->maxpage = r->npage;
  170.         }
  171.          }
  172.       } else {
  173.          p = (unsigned char *)0;
  174.       }
  175.       if (!p) {
  176.          if (r->handle < 0) {
  177.         if ((r->handle = CREATE(r->name)) < 0) return EOF;
  178.      }
  179.      if (write(r->handle, (char*)((r->page)[r->maxpage]), PAGESIZE) !=
  180.          PAGESIZE)
  181.         return EOF;
  182.       }
  183.       r->poz = 0;
  184. #ifdef TRACE
  185.       Printf("  roll 2: poz = %4d page = %d max = %2d handle = %d\n",
  186.                         r->poz,   r->npage, r->maxpage, r->handle);
  187. #endif
  188.    }
  189.    *((r->page)[r->npage] + (r->poz)++) = i;
  190. #ifdef TRACE
  191.    if (r->npage < 0 || r->npage >= MAXPAGE || r->npage > r->maxpage) {
  192.       (void)fprintf(myout, "Alarm! npage out of range\n");
  193.    }
  194.    if (r->poz < 0 || r->poz > PAGESIZE) {
  195.       (void)fprintf(myout, "Alarm! poz out of range\n");
  196.    }
  197. #endif
  198.    return 0;
  199. }
  200.  
  201. int rgetc()
  202. {
  203.    if (r->poz >= PAGESIZE) {
  204. #ifdef TRACE
  205.       Printf("  roll 1: poz = %4d page = %d max = %2d handle = %d\n",
  206.                        r->poz,   r->npage, r->maxpage, r->handle);
  207. #endif
  208.       if (r->handle >= 0 && (r->npage)+1 >= r->maxpage) {
  209.      if (read(r->handle, (char*)((r->page)[r->maxpage]), PAGESIZE) !=
  210.          PAGESIZE) {
  211.         return EOF;
  212.      }
  213.      r->npage = r->maxpage;
  214.       } else {
  215.      if (r->handle < 0 && r->npage >= MAXPAGE) return EOF;
  216.      if (!(r->page)[++(r->npage)]) return EOF;
  217.       }
  218.       r->poz = 0;
  219. #ifdef TRACE
  220.       Printf("  roll 2: poz = %4d page = %d max = %2d handle = %d\n",
  221.                         r->poz,   r->npage, r->maxpage, r->handle);
  222. #endif
  223.    }
  224. #ifdef TRACE
  225.    if (r->npage < 0 || r->npage >= MAXPAGE || r->npage > r->maxpage) {
  226.       (void)fprintf(myout, "Alarm! npage out of range\n");
  227.    }
  228.    if (r->poz < 0 || r->poz >= PAGESIZE) {
  229.       (void)fprintf(myout, "Alarm! poz out of range\n");
  230.    }
  231. #endif
  232.    return *((r->page)[r->npage] + (r->poz)++);
  233. }
  234.